/**
 * @author yxw
 * date : 2013年9月30日 下午3:50:36 
 */
package com.tzinfo.tzb;

import java.util.Calendar;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.galhttprequest.GalHttpRequest;
import com.galhttprequest.GalHttpRequest.GalHttpLoadTextCallBack;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tzinfo.data.TztData;
import com.tzinfo.tzb.domain.DefaultResult;
import com.tzinfo.tzb.domain.Tzb_UserInfoModel.Userinfo;
import com.tzinfo.tzb.util.TzbCommonData;
import com.yxw.util.OutLog;
import com.yxw.util.YXWDateUtil;
import com.yxw.view.util.ResFinder;

public class TzbMyModifyActivity extends TzbBaseActivity {

	// 标志
	private int userstate_num = 0, bbsex_num = 0;

	// 数据

	// UI组件
	private TextView text_userbirthday, text_bbbirthday;
	private EditText text_netname, text_signature;
	private LinearLayout layout_baby_info;
	private Spinner spinner_userstate, spinner_bbsex;

	// 适配器
	private ArrayAdapter<String> adapter, adapterBBSex;

	// 其他
	private static String[] sexStr = {"男宝宝", "女宝宝" };
	private static String[] userstateStr = { "未婚", "有宝宝", "准妈妈", "备孕中", "其他" };

	public static TzbMyModifyActivity activity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		activity = this;
		setContentView(ResFinder.getR(this, "R.layout.tzb_ac_modify_userinfo"));
		init();
		initData();
	}

	private void init() {
		text_userbirthday = (TextView) ResFinder.findViewById(this,
				"R.id.text_userbirthday");
		text_bbbirthday = (TextView) ResFinder.findViewById(this,
				"R.id.text_bbbirthday");
		text_netname = (EditText) ResFinder.findViewById(this,
				"R.id.text_netname");
		text_signature = (EditText) ResFinder.findViewById(this,
				"R.id.text_signature");
		layout_baby_info = (LinearLayout) ResFinder.findViewById(this,
				"R.id.layout_baby_info");
		spinner_userstate = (Spinner) ResFinder.findViewById(this,
				"R.id.spinner_userstate");
		spinner_bbsex = (Spinner) ResFinder.findViewById(this,
				"R.id.spinner_bbsex");
		// 将可选内容与ArrayAdapter连接起来
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, userstateStr);
		adapterBBSex = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, sexStr);
		// 设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapterBBSex
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 将adapter 添加到spinner中
		spinner_userstate.setAdapter(adapter);
		spinner_bbsex.setAdapter(adapterBBSex);
		spinner_userstate
				.setOnItemSelectedListener(new SpinnerSelectedListener(
						SpinnerSelectedListener.USER_STATE));
		spinner_bbsex.setOnItemSelectedListener(new SpinnerSelectedListener(
				SpinnerSelectedListener.BABY_SEX));
	}

	public void initData() {
		if (TzbCommonData.userInfoModel != null) {
			Userinfo userinfo = TzbCommonData.userInfoModel.getData();
			text_userbirthday.setText(YXWDateUtil.getDateFormat(
					userinfo.getUserbirthday(), "yyyy-MM-dd"));
			text_netname.setText(userinfo.getNetname());
			text_signature.setText(userinfo.getSignature());
			int i = 0;
			for (i = userstateStr.length - 1; i > 0; i--) {
				if (userstateStr[i].equals(userinfo.getUserstate().trim())) {
					break;
				}
			}
			userstate_num = i;
			spinner_userstate.setSelection(userstate_num);
			if (userstate_num == 1) {
				int j = 0;
				for (j = sexStr.length - 1; j > 0; j--) {
					if (sexStr[j].equals(userinfo.getBbsex().trim())) {
						break;
					}
				}
				bbsex_num = j;
				spinner_bbsex.setSelection(bbsex_num);
				text_bbbirthday.setText(YXWDateUtil.getDateFormat(
						userinfo.getBbbirthday(), "yyyy-MM-dd"));
				layout_baby_info.setVisibility(View.VISIBLE);
			} else {
				layout_baby_info.setVisibility(View.GONE);
			}
		}
	}

	// onclick
	public void submitRequest(View v) {
		dialog.show();
		String url = ResFinder.getResString(this,
				"R.string.tzb_url_Tzb_UserInfoAdd")
				+ "?sessionid="
				+ TztData.userInfo.getData().getSessionID()
				+ "&netname="
				+ text_netname.getText().toString()
				+ "&signature="
				+ text_signature.getText().toString()
				+ "&userstate="
				+ spinner_userstate.getItemAtPosition(userstate_num)
				+ "&userbirthday=" + text_userbirthday.getText().toString();
		if (userstate_num == 1) {
			url += "&bbbirthday=" + text_bbbirthday.getText().toString()
					+ "&bbsex=" + spinner_bbsex.getItemAtPosition(bbsex_num);
		}
		OutLog.LogE("yxw", url);
		GalHttpRequest galHttpRequest = new GalHttpRequest(this);
		galHttpRequest.setCacheEnable(false);
		galHttpRequest.setURL(url);
		galHttpRequest.startAsynRequestString(new GalHttpLoadTextCallBack() {

			@Override
			public void textLoaded(String arg0) {
				OutLog.LogE("yxw", "json:" + arg0);
				Gson gson = new Gson();
				java.lang.reflect.Type type = new TypeToken<DefaultResult>() {
				}.getType();
				DefaultResult result = gson.fromJson(arg0, type);
				OutLog.LogE("yxw", "result:" + result.toString());
				if (result.isSuccess()) {
					Toast.makeText(TzbMyModifyActivity.this, "上传成功", 1).show();
					TzbTabMyActivity.reflashInfo = true;
					TzbMyModifyActivity.this.finish();
				} else {
					Toast.makeText(TzbMyModifyActivity.this, "上传失败", 1).show();
				}
				dialog.dismiss();
			}
		});
	}

	// onclick
	public void showDatePicker(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final TextView dateTextView = (TextView) v;
		final DatePicker datePicker = new DatePicker(this);
		builder.setView(datePicker);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH), null);

		builder.setTitle("请选择生日日期");
		builder.setPositiveButton("确  定",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						StringBuffer sb = new StringBuffer();
						sb.append(String.format("%d-%02d-%02d",
								datePicker.getYear(),
								datePicker.getMonth() + 1,
								datePicker.getDayOfMonth()));
						dateTextView.setText(sb.toString());
						dialog.cancel();
					}
				});

		Dialog dialog = builder.create();
		dialog.show();
	}

	// 使用数组形式操作
	class SpinnerSelectedListener implements OnItemSelectedListener {
		public final static int USER_STATE = 1;
		public final static int BABY_SEX = 0;
		private int flag;

		public SpinnerSelectedListener(int flag) {
			this.flag = flag;
		}

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (flag == USER_STATE) {
				if (arg2 == USER_STATE) {
					layout_baby_info.setVisibility(View.VISIBLE);
				} else {
					layout_baby_info.setVisibility(View.GONE);
				}
				userstate_num = arg2;
			} else {
				bbsex_num = arg2;
			}

		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
}
